package com.tdzx.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.tdzx.R;


public class MaskImage extends ImageView{
	int mImageSource=0;
	int mMaskSource=0; 
	RuntimeException mException;

	public MaskImage(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MaskImage, 0, 0);
		mImageSource = a.getResourceId(R.styleable.MaskImage_image, 0);
		mMaskSource = a.getResourceId(R.styleable.MaskImage_mask, 0);

		/*if (mImageSource == 0 || mMaskSource == 0) {
			mException = new IllegalArgumentException(a.getPositionDescription() + 
					": The content attribute is required and must refer to a valid image.");
		}

		if (mException != null) 
			throw mException;
       *//**
        * 主要代码实现
        *//*
		//获取图片的资源文�?
		Bitmap original = BitmapFactory.decodeResource(getResources(), mImageSource);
		Matrix matrix = new Matrix();
		
		//获取遮罩层图�?
		Bitmap mask = BitmapFactory.decodeResource(getResources(), mMaskSource);
		
		float f_yScale = ((float)mask.getHeight()) / original.getHeight();
		float f_xScale = ((float)mask.getWidth()) /original.getWidth();
		matrix.postScale(f_xScale, f_yScale);
		original = Bitmap.createBitmap(original, 0, 0, original.getWidth(), original.getHeight(),matrix,true);
		
		Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
	     //将遮罩层的图片放到画布中
		Canvas mCanvas = new Canvas(result);
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
		mCanvas.drawBitmap(original, 0, 0, null);
		mCanvas.drawBitmap(mask, 0, 0, paint);
		paint.setXfermode(null);
		setImageBitmap(result);
//		setBackgroundDrawable(new BitmapDrawable(result));
		setScaleType(ScaleType.CENTER); */
		
		a.recycle();
		
		setOnTouchListener(TouchDark);
	}
	
	public void setBitmap(Bitmap bitmap) {
		Matrix matrix = new Matrix();
		
		//获取遮罩层图�?
		Bitmap mask = BitmapFactory.decodeResource(getResources(), mMaskSource);
		
		float f_yScale = ((float)mask.getHeight()) / bitmap.getHeight();
		float f_xScale = ((float)mask.getWidth()) /bitmap.getWidth();
		matrix.postScale(f_xScale, f_yScale);
		bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),matrix,true);
		
		Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
	     //将遮罩层的图片放到画布中
		Canvas mCanvas = new Canvas(result);
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
		mCanvas.drawBitmap(bitmap, 0, 0, null);
		mCanvas.drawBitmap(mask, 0, 0, paint);
		paint.setXfermode(null);
		setImageBitmap(result);
//		setBackgroundDrawable(new BitmapDrawable(result));
		setScaleType(ScaleType.CENTER); 
		
//		setOnTouchListener(TouchDark);
		requestLayout();
	}
	
	public final OnTouchListener TouchLight = new OnTouchListener() {

		public final float[] BT_SELECTED = new float[] {
				1, 0, 0, 0, 50, 0, 1, 0, 0, 50, 0, 0, 1, 0, 50, 0, 0, 0, 1, 0
		};
		public final float[] BT_NOT_SELECTED = new float[] {
				1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0
		};

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
//				getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));
//				setBackgroundDrawable(v.getBackground());
				getDrawable().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));
				setImageDrawable(getDrawable());
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
//				getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));
//				setBackgroundDrawable(v.getBackground());
				getDrawable().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));
				setImageDrawable(getDrawable());
			}
			return false;
		}
	};
	
	public final OnTouchListener TouchDark = new OnTouchListener() {

		public final float[] BT_SELECTED = new float[] {
				1, 0, 0, 0, -50, 0, 1, 0, 0, -50, 0, 0, 1, 0, -50, 0, 0, 0, 1, 0
		};
		public final float[] BT_NOT_SELECTED = new float[] {
				1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0
		};

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			System.err.println(event.getAction() + "!!!!!!!!!!!!");
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
//				getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));
//				setBackgroundDrawable(v.getBackground());
				
				getDrawable().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));
				setImageDrawable(getDrawable());
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
//				getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));
//				setBackgroundDrawable(v.getBackground());
				getDrawable().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));
				setImageDrawable(getDrawable());
//				v.dispatchTouchEvent(event);
			} else if(event.getAction() == MotionEvent.ACTION_CANCEL) {
				getDrawable().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));
				setImageDrawable(getDrawable());
			} 
			return false;
		}
	};
}
